package com.tcs.swing.action;

<<<<<<< HEAD
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
=======
>>>>>>> 4019f38fe947fdf951556f50ab3f96ea4c76af61
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

<<<<<<< HEAD
=======
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

>>>>>>> 4019f38fe947fdf951556f50ab3f96ea4c76af61
public class RepAction implements ActionListener {
	
	private final Logger logger = LoggerFactory.getLogger(RepAction.class);
	
	private JTextField pathText;
	
	private JTextField repStrText;
	
	private JTextField destStrText;
	
	private JRadioButton addRadioButton;

	private JRadioButton repRadioButton;
	
	public RepAction() {
		super();
	}

	public RepAction(JTextField pathText, JTextField repStrText) {
		super();
		this.pathText = pathText;
		this.repStrText = repStrText;
	}

	public RepAction(JTextField pathText, JTextField repStrText, JRadioButton addRadioButton,
			JRadioButton repRadioButton) {
		super();
		this.pathText = pathText;
		this.repStrText = repStrText;
		this.addRadioButton = addRadioButton;
		this.repRadioButton = repRadioButton;
	}

	public RepAction(JTextField pathText, JTextField repStrText, JTextField destStrText, JRadioButton addRadioButton,
			JRadioButton repRadioButton) {
		super();
		this.pathText = pathText;
		this.repStrText = repStrText;
		this.destStrText = destStrText;
		this.addRadioButton = addRadioButton;
		this.repRadioButton = repRadioButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		new Thread("repAction Thread") {
			
			@Override
			public void run() {
				String path = pathText.getText();
				String repStr = repStrText.getText();
				String destStr = destStrText.getText();
				path = path.replace("\\", "\\\\");
				logger.info("文件路径: {} , 替换 : {} " , path , repStr);
				readfile(path, repStr , destStr);
				JOptionPane.showMessageDialog(new JPanel(), "执行成功!", "标题",JOptionPane.WARNING_MESSAGE);
			}
		}.start();
	}
	
	/**
	 * 读取某个文件夹下的所有文件
	 */
	public boolean readfile(String filepath, String repStr , String destStr)  {
		boolean result = false;
		try {
			File file = new File(filepath);
			logger.info("filepath : {} , repStr : {} , destStr : {} " , filepath, repStr, destStr);
			operationFile(file, filepath, repStr, destStr, result);
		} catch (FileNotFoundException e) {
			logger.error("" , e);
		} catch (IOException e) {
			logger.error("" , e);
		}
		return true;
	}
	
	/**
	 * 操作File
	 * @param file
	 * @param filepath
	 * @param repStr
	 * @param result
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private void operationFile(File file , String filepath , String repStr , String destStr, boolean result) throws FileNotFoundException, IOException {
		String[] filelist = file.list();
		if (null != filelist && filelist.length > 0) {
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(filepath + "\\" + filelist[i]);
				if (!readfile.isDirectory()) {
					if (repRadioButton.isSelected())
						result = readfile.renameTo(new File(readfile.getPath().replace(repStr, destStr)));
					else if (addRadioButton.isSelected())
						result = readfile.renameTo(new File(filepath + "\\" + repStr + readfile.getName()));
					logger.info(" 文件 : {} , 路径: {} , 是否成功 : {} " , readfile.getName() , readfile.getPath() ,  result);
				} else if (readfile.isDirectory()) {
<<<<<<< HEAD
					String tempPath = "";
					if (addRadioButton.isSelected()) {
						tempPath = repStr + readfile.getName();
						readfile.renameTo(new File(filepath + "\\" + tempPath));
					}
					this.readfile(filepath + "\\" + tempPath , repStr , destStr);
=======
					this.readfile(filepath + "\\" + filelist[i], destStr , repStr);
>>>>>>> 4019f38fe947fdf951556f50ab3f96ea4c76af61
				}
			}
		}
	}

}
